package com.lc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.mapper.TRoleMapper;
import com.guigu.mapper.TUserMapper;
import com.guigu.mapper.TUserRoleMapper;
import com.guigu.pojo.TRole;
import com.guigu.pojo.TUser;
import com.guigu.viwe.PageVo;
import com.lc.service.SysUserService;
import com.lc.vo.loginroleandshouroleView;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysUserServiceimpl implements SysUserService {


    @Resource
    private TUserMapper tUserMapper;

    @Resource
    private TRoleMapper tRoleMapper;

    @Resource
    private TUserRoleMapper tUserRoleMapper;


    @Override
    public PageInfo<TUser> queryalltuserpageService(PageVo<TUser> pageVo) {
        PageHelper.startPage(pageVo.getCurrentPage(),pageVo.getPageSize());
        PageInfo<TUser> info=new PageInfo<>(this.tUserMapper.queryalltuserpage(pageVo.getCondition()));
        return info;
    }

    @Override
    public void adduserService(TUser user) {
        user.setStatus(0);
        this.tUserMapper.insert(user);
    }

    @Override
    public loginroleandshouroleView selectloginroleService(int loginid, int shouid) {
        //查询登录用户拥有的角色
        loginroleandshouroleView v=new loginroleandshouroleView();
        List<TRole> loginrole = this.tRoleMapper.selecroleByuserid(loginid);
        List<TRole> shourole = this.tRoleMapper.selecroleByuserid(shouid);
        v.setLoginrole(loginrole);
        v.setShouid(shourole);
        return v;
    }

    @Override
    public void updateuserService(TUser tUser) {
         this.tUserMapper.updateByPrimaryKeySelective(tUser);
    }

    @Override
    public void authorizationuserService(loginroleandshouroleView view) {
        this.tUserRoleMapper.deletefromtuserrolebyuserid(view.getAid());
        if(!view.getSid().isEmpty()){
            this.tUserRoleMapper.inserttuserrolebyuseridanroleid(view);
        }

    }

    @Override
    public int deleteService(int id) {
        this.tUserMapper.deleteByPrimaryKey(id);
        return 1;
    }

    @Override
    public TUser loginuserService(int id) {
        return this.tUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public int yanzshoujihaoheyhmService(String username, String phone) {
        TUser u=new TUser();
        u.setUsername(username);
        u.setTelephone(phone);
        int selectshoujihaoheusername = this.tUserMapper.selectshoujihaoheusername(u);
        if(selectshoujihaoheusername==1){
            return 1;
        }
        return 0;
    }

    @Override
    public int xiugaimmzService(TUser user) {
        this.tUserMapper.updateshoujihaoanduswername(user);
        return 1;
    }
}
